﻿using MediatR;
using Microsoft.SqlServer.Server;
using SmartCity.ProcessEngine.API.Read.Application.Commands.Settings;
using SmartCity.ProcessEngine.API.Read.DTO;
using SmartCity.ProcessEngine.Domain;
using SmartCity.ProcessEngine.Errorcode;
using SmartCity.ProcessEngine.Infrastructure.Extensions;
using SmartCity.ProcessEngine.Infrastructure.Interfaces;

namespace SmartCity.ProcessEngine.API.Read.Application.CommandHandlers.Settings
{
    public class GetSettingCommandHandler:IRequestHandler<GetSettingCommand,ApiResult<ApiPaging<SettingDto>>>
    {
        private readonly IBaseRepository<WF_Form> formRep;
        private readonly IBaseRepository<WF_FromType> typeRep;
        private readonly IBaseRepository<WF_Setting> settingRep;
        private readonly IBaseRepository<Industry> industryRep;

        public GetSettingCommandHandler(IBaseRepository<WF_Form> formRep,IBaseRepository<WF_FromType> typeRep,IBaseRepository<WF_Setting> settingRep,IBaseRepository<Industry> industryRep)
        {
            this.formRep = formRep;
            this.typeRep = typeRep;
            this.settingRep = settingRep;
            this.industryRep = industryRep;
        }

        public Task<ApiResult<ApiPaging<SettingDto>>> Handle(GetSettingCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<SettingDto>> result = new ApiResult<ApiPaging<SettingDto>>();
            result.code = ApiEnum.SearchSuccess;
            result.message = ApiEnum.SearchSuccess.GetDescription();

            var list = from a in settingRep.GetAll()
                       join b in formRep.GetAll() on a.FormNo equals b.FormNo
                       join c in typeRep.GetAll() on b.TypeNo equals c.TypeNo
                       join d in industryRep.GetAll() on c.IndustryId equals d.IndustryId
                       select new SettingDto
                       {
                           SettingId = a.SettingId,
                           NodeNo = a.NodeNo,
                           NodeName = a.NodeName,
                           NextNodeNo = a.NextNodeNo,
                           PrevNodeNo = a.PrevNodeNo,
                           UserId = a.UserId,
                           UserNickname = a.UserNickname,
                           FormNo = b.FormNo,
                           FormName = b.FormName,
                           TypeNo = c.TypeNo,
                           TypeName = c.TypeName,
                           IndustryId = d.IndustryId,
                           IndustryName = d.IndustryName,
                       };
            if (request.IndustryId > 0)
            {
                list = list.Where(x => x.IndustryId == request.IndustryId);
            }
            if (!string.IsNullOrEmpty(request.FormNo))
            {
                list = list.Where(x => x.FormNo == request.FormNo);
            }
            if (!string.IsNullOrEmpty(request.TypeNo))
            {
                list = list.Where(x => x.TypeNo == request.TypeNo);
            }

            int ListCount = list.Count();
            int PageCount = (int)Math.Ceiling((ListCount * 1.0 / request.PageSize));
            list = list.OrderBy(x => x.SettingId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);

            ApiPaging<SettingDto> paging= new ApiPaging<SettingDto>();
            paging.totalCount = ListCount;
            paging.totalPage = PageCount;
            paging.data = list.ToList();

            result.data = paging;

            return Task.FromResult(result);
        }
    }
}
